package demo;
class MyBlockQueue{
    private String[] data = null;
    private volatile int head = 0;
    private volatile int tail = 0;
    private volatile int size = 0;
    private Object locker = new Object();
    public  MyBlockQueue(int capacity) {
        data = new String[capacity];

    }
    public void put(String s) throws InterruptedException {
        synchronized (locker){
            if(size == data.length){
                //队列满了
               // return;
                locker.wait();
            }
            data[tail] = s;
            tail++;
            if(tail >= data.length){
                tail = 0;
            }
            size++;
            locker.notify();

        }

    }
    public String take() throws InterruptedException {
        String ret = "";
        synchronized (locker){
            if (size == 0){
                //队列为空
              //  return null;
                locker.wait();
            }
           ret = data[head];
            head++;
            if(head >= data.length){
                head = 0;
            }
            size--;
            locker.notify();
        }
        return ret;


    }
}
public class Test2 {

}
